<?php

namespace Frontend\Controllers;

use Models\Doctypes;
use Models\Topics;
use Models\References;

class TypeController extends ControllerBase {

    public function indexAction($id) {
        
    }

    public function searchAction($id) {
        $text_search = $this->request->getPost('text_search');
        $currentPage = (int) $this->request->getPost('page');
        if (!$currentPage) {
            $currentPage = 1;
        }
        $limit = (int) $this->request->getPost('limit');
        if (!$limit) {
            $limit = 10;
        }
        $limit_days = References::findFirstById('limit_in_day');
        if ($limit_days) {
            $limit_in_day = $limit_days->value;
        } else {
            $limit_in_day = 1;
        }
        $type = Doctypes::findFirstById($id);
        $topics = Topics::find("doctype_id = '$id'");
        if (count($topics)) {
            $topicss_id = array();
            foreach ($topics as $value) {
                $topicss_id[] = $value->id;
            }
            $topics_str = implode(",", $topicss_id);
            $query = $this->modelsManager->createBuilder()
                    ->columns(array(
                        'Models\Documents.id',
                        'Models\Documents.title',
                        'Models\Documents.description',
                        'Models\Documents.submit_date',
                        'Models\Documents.cost',
                        'Models\Documents.link',
                        'Models\Documents.image',
                        'Models\Documents.topic_id',
                        'Models\Documents.download',
                        'Models\Documents.discount',
                        'Topics.name TopicName',
                    ))
                    ->from('Models\Documents')
                    ->leftjoin('\Models\Topics', 'Topics.id = Models\Documents.topic_id', 'Topics')
                    ->where("topic_id IN ($topics_str)")
                    ->andwhere('Models\Documents.public = 1');

            $docs_in_day = $query->leftjoin('Models\Downloads', 'Downloads.doc_id = Models\Documents.id', 'Downloads')
                            ->groupby('Models\Documents.id')
                            ->orderby('Models\Documents.download DESC')
                            ->limit($limit_in_day)
                            ->getQuery()->execute();

            $query1 = $this->modelsManager->createBuilder()
                    ->columns(array(
                        'Models\Documents.id',
                        'Models\Documents.title',
                        'Models\Documents.description',
                        'Models\Documents.submit_date',
                        'Models\Documents.cost',
                        'Models\Documents.link',
                        'Models\Documents.image',
                        'Models\Documents.topic_id',
                        'Models\Documents.download',
                        'Models\Documents.discount',
                        'Topics.name TopicName',
                    ))
                    ->from('Models\Documents')
                    ->leftjoin('\Models\Topics', 'Topics.id = Models\Documents.topic_id', 'Topics')
                    ->where("topic_id IN ($topics_str)")
                    ->andwhere('Models\Documents.public = 1');
            if ($this->request->getPost('text_search')) {
                $query1->andWhere("title  LIKE '%$text_search%' OR description  LIKE '%$text_search%'");
            }
            $docs = $query1->getQuery()->execute();
        } else {
            $docs = array();
            $docs_in_day = array();
        }
        $pagintion = new \Phalcon\Paginator\Adapter\Model(
                array(
            "data" => $docs,
            "limit" => $limit,
            "page" => $currentPage
                )
        );
        $this->view->title = 'VIDS - ' . $type->name;
        $this->view->type = $type;
        $this->view->topics = $topics;
        $this->view->docs = $pagintion->getPaginate();
        $this->view->text_search = $text_search;
        $this->view->page = $currentPage;
        $this->view->limit = $limit;
        $this->view->docs_in_day = $docs_in_day;
    }

    function getalltopicsAction() {
        $result = "<option value=''>Chọn đề tài</option>";
        if ($this->request->getPost('id')) {
            $topics = Topics::findByDoctype_id($this->request->getPost('id'));
            foreach ($topics as $topic) {
                $result .="<option value='$topic->id'>$topic->name</option>";
            }
        }
        $this->view->disable();
        echo $result;
    }

}
